/**
 * 常用JS变量:
 * agentEvent = 代理模式下自动点击模块
 * acEvent= 无障碍模式下自动点击模块
 * device = 设备信息模块
 * file = 文件处理模块
 * http = HTTP网络请求模块
 * shell = shell命令模块
 * thread= 多线程模块
 * image = 图色查找模块
 * utils= 工具类模块
 * global = 全局快捷方式模块
 * 常用java变量：
 *  context : Android的Context对象
 *  javaLoader : java的类加载器对象
 * 导入Java类或者包：
 *  importClass(类名) = 导入java类
 *      例如: importClass(java.io.File) 导入java的 File 类
 *  importPackage(包名) =导入java包名下的所有类
 *      例如: importPackage(java.util) 导入java.util下的类
 *
 */

toast("即将开始运行程序");
sleep(1000);

beforeStartUp();

//////////////////////////////  开始工作啦  全局变量 ////////////////////////////////////////
// 全局变量
var screenWidth = getScreenWidth();
logd("current phone screen width :" + screenWidth);
var screenHeight = getScreenHeight();
logd("current phone screen height :" + screenHeight);


// 当前关注列表中的第一个人
// var currentNode = getAttentionListByDrawingOrder();
// 当前任务中的关注列表
var clickedCustomer = [];
// 点击的次数
var clickedTime = 0;
// 根据父节点信息找到的子节点
var currentChildByFNode = new NodeInfo();

/**
 * 需要在前台进行手动设置
 * 置顶的点赞数量
 *
 * @type {number}
 */
var loveNumber = 600;
// 左右滑动的间隔时间，目前为 1秒钟滑动一次
var moveToLeftOrRightIntervalTime = 1000;
// 上下滑动的间隔时间，目前为 1秒钟滑动一次
var moveToUpOrDownIntervalTime = 1000;

/**
 * 详情页面的等待时间 ,默认为30秒
 * 需要在前台进行手动设置
 *
 * @type {number}
 */
//
var viewProductDetailTotalTime = 1000 * 10;

/**
 * 收藏+点赞 或者 点赞车， 默认为false，只有点赞
 *
 * @type {boolean}
 */
var isLoveOrCollectArticle = false;

var global_albumName = "";

/**
 * 新文的点赞数一般比较少，可以直接跳过
 *
 * @type {number}
 */
var global_newContentLoveCount = 2;
//////////////////////////////  开始工作啦  全局变量  ////////////////////////////////////////


// 读取变量
readConfig = function () {
    try {
        var txt_newContentLoveCount = readConfigString("newContentLoveCount");
        if (txt_newContentLoveCount != null && txt_newContentLoveCount !== '') {
            global_newContentLoveCount = txt_newContentLoveCount;
        }
        logi("疑似新文点赞数大于 " + global_newContentLoveCount + "，就不会点击");

        var txt_loveNumber = readConfigString("loveNumber");
        if (txt_loveNumber != null && txt_loveNumber !== '') {
            loveNumber = txt_loveNumber;
        }
        logi("点赞数大于 " + loveNumber + "，就不会点击");

        var txt_viewProductDetailTotalTime = readConfigString("viewProductDetailTotalTime");
        if (txt_viewProductDetailTotalTime != null && txt_viewProductDetailTotalTime !== '') {
            viewProductDetailTotalTime = txt_viewProductDetailTotalTime * 1000;
        }
        logi("距离详情页面操作等待时间 : " + viewProductDetailTotalTime);


        var txt_attentionAndLikeWaitTime = readConfigString("attentionAndLikeWaitTime");
        if (txt_attentionAndLikeWaitTime != null && txt_attentionAndLikeWaitTime !== '') {
            attentionAndLikeWaitTime = txt_attentionAndLikeWaitTime * 1000;
        }
        logi("关注和点赞间隔时长 : " + viewProductDetailTotalTime);

        var txt_likeAndCollectWaitTime = readConfigString("likeAndCollectWaitTime");
        if (txt_likeAndCollectWaitTime != null && txt_likeAndCollectWaitTime !== '') {
            likeAndCollectWaitTime = txt_likeAndCollectWaitTime * 1000;
        }
        logi("点赞和收藏间隔时长 : " + viewProductDetailTotalTime);

        var txt_isLoveOrCollectArticle = readConfigString("isLoveOrCollectArticle");
        if (txt_isLoveOrCollectArticle != null && txt_isLoveOrCollectArticle !== ''
            && txt_isLoveOrCollectArticle === "true") {
            isLoveOrCollectArticle = true;
        } else {
            isLoveOrCollectArticle = false;
        }
        isLoveOrCollectArticle = true;
        logi("是否需要开启收藏功能 : " + isLoveOrCollectArticle);

        var txt_global_albumName = readConfigString("albumName");
        if (txt_global_albumName != null && txt_global_albumName !== '') {
            global_albumName = txt_global_albumName;
        }
        logi("加入的专辑名称 : " + global_albumName);

    } catch (e) {
        loge("invoke readConfig error: ", e);
        exit();
    }
};
// 读取配置信息
readConfig();

logi("检查配置！");
sleep(1000 * 2);

function getAttentionListByDrawingOrder() {
    try {
        // 获取当前的点击列表
        var nodeInfo = clz("android.widget.LinearLayout").depth(14).getOneNodeInfo(2000);
        if (nodeInfo != null) {
            return nodeInfo;
        } else {
            loge("无障碍模式");
            exit();
        }
    } catch (e) {
        loge("getAttentionListByDrawingOrder error {}", e);
        exit();
    }
}

function getAttentionNodeByNextSiblings(currentNodeTemp) {
    try {
        if (null != currentNodeTemp) {
            var sons = currentNodeTemp.nextSiblings();
            if (null != sons && currentNodeTemp.nextSiblings().length > 0) {
                return currentNodeTemp.nextSiblings()[0];
            }
            return null;
        }
    } catch (e) {
        loge("getAttentionNodeByNextSiblings error {}", e);
        exit();
    }
}

function setChildNodeByClz(fNode, sonNodeClz, sonNodeDepth, drawingOrder, index) {
    try {
        if (null != fNode) {
            var children = fNode.allChildren();
            if (null != children && children.length > 0) {

                for (var i = 0; i < children.length; i++) {
                    if (children[i].clz === sonNodeClz
                        && children[i].depth === sonNodeDepth
                        && children[i].drawingOrder === drawingOrder
                        && children[i].index === index) {
                        currentChildByFNode = children[i];
                        break;
                    } else {
                        setChildNodeByClz(children[i], sonNodeClz, sonNodeDepth, drawingOrder, index);
                    }
                }
            } else {
                // logd("-1111--" +currentChildByFNode)
                // currentChildByFNode = null;
                // logw("该节点下没有子节点了。。");
            }
        } else {
            currentChildByFNode = null;
            loge("节点不能为 null。。")
        }
    } catch (e) {
        currentChildByFNode = null;
        loge("setChildNodeByClz error " + e);
    }
}

/**
 * 设置当前点击的用户
 *
 * @param info
 * @param node
 */
setCurrentCustomer = function (info) {
    setChildNodeByClz(info.attention_list_currentNodeInfo,
        "android.widget.TextView", 17, 1, 0);
    clickedCustomer.push({"id": clickedTime + 1, "name": currentChildByFNode.text})
    info.allCustomerArray.push({"id": clickedTime + 1, "name": currentChildByFNode.text});
    info.currentAccountName = currentChildByFNode.text;
    info.currentAccountNumber = clickedTime + 1;
};

/**
 * 获取当前父节点的
 */
var quick_parentNodeByChildNode;
getParentNodeByChildNode = function (childNode, level) {
    try {
        quick_parentNodeByChildNode = null;
        var temp = childNode.parent();
        for (var i = 0; i < level - 1; i++) {
            temp = temp.parent();
        }
        quick_parentNodeByChildNode = temp;
    } catch (e) {
        loge("getParentNodeByChildNode {}", e);
    }
};


quickToGetCorrectContentToClick_findTOP = function (info) {
    // 先找置顶的 只会存在一个【置顶】
    var topNodeInfo = clz("android.widget.TextView").depth(20).drawingOrder(5)
        .index(1).getNodeInfo(2000);
    logd("00");
    if (topNodeInfo != null) {
        var valueTemp = null;// 置顶的按钮
        for (var i = 0; i < topNodeInfo.length; i++) {
            if (topNodeInfo[i] != null && topNodeInfo[i].text === "置顶") {
                valueTemp = topNodeInfo[i];
                break;
            }
        }
        logd("12");
        if (valueTemp != null && valueTemp.text === "置顶") {
            info.personal_center_topIsExist = true;
            logd("2");
            // 先查找父元素
            getParentNodeByChildNode(valueTemp, 3);
            // 根据父元素查找子元素
            currentChildByFNode = null;// 这个就是love count node
            setChildNodeByClz(quick_parentNodeByChildNode
                , "android.widget.TextView", 20, 1, 1);

            if (currentChildByFNode != null) {
                logd("3");
                var loveCount = currentChildByFNode.text;
                logd("loveCount " + loveCount)
                if (loveCount != null && (loveCount < loveNumber || loveCount === "赞")
                    && !currentChildByFNode.previousSiblings()[0].selected
                    && loveCount.indexOf("万") === -1) {
                    logd("4");
                    // // 查找父元素，进行点击
                    // getParentNodeByChildNode(topNodeInfo, 4);
                    if (quick_parentNodeByChildNode != null) {
                        logd("5");
                        logi("点击【置顶】文，点赞数：" + loveCount)
                        quick_parentNodeByChildNode.click();
                        info.personal_center_topIsClicked = true;
                    } else {
                        logd("7")
                    }
                }
            } else {
                logd("6")
                // 需要再往下滑动
                moveToScreenHeightHalf(screenHeight / 4 * 3);
                quickToGetCorrectContentToClick_findTOP();
            }

        }
    }
};

quickToGetCorrectContentToClick_findNormal = function (info) {

    personal_center_normal_toGetRightProducts(info);
    //
    // // 点赞数量
    // var loveNodeInfos = clz("android.widget.TextView").depth(20)
    //     .drawingOrder(1).index(1).getNodeInfo(2000);
    // if (loveNodeInfos != null) {
    //     logd("99999")
    //     var index = 0;
    //     if (info.personal_center_topIsExist) {
    //         index = 1;
    //     }
    //     for (var i = index; i < loveNodeInfos.length; i++) {
    //         var value = loveNodeInfos[i].text;
    //         // 新文一般不能点击 【赞】不能点击
    //         if (value != null && !isNaN(value)
    //             && parseInt(value) > global_newContentLoveCount
    //             && !loveNodeInfos[i].previousSiblings()[0].selected) {
    //             logi("点击文章，点赞数为：" + parseInt(value));
    //             info.personal_center_productToClick = loveNodeInfos[i];
    //             break;
    //         }
    //     }
    //
    //     if (info.personal_center_productToClick != null) {
    //         // 查找父元素，进行点击
    //         getParentNodeByChildNode(info.personal_center_productToClick, 2);
    //
    //         if (quick_parentNodeByChildNode != null) {
    //             quick_parentNodeByChildNode.click();
    //             logd("12345678");
    //         } else {
    //             sleep(1000)
    //             logd("12000");
    //             moveToScreenHeightHalf(screenHeight / 4 * 3);
    //             quickToGetCorrectContentToClick_findNormal(info);
    //         }
    //         logi("popo")
    //     } else {
    //         sleep(1000)
    //         logd("has no current love button, need to move")
    //         moveToScreenHeightHalf(screenHeight / 4 * 3);
    //         quickToGetCorrectContentToClick_findNormal(info);
    //     }
    // } else {
    //     sleep(1000)
    //     logd("has no love button, need to move")
    //     moveToScreenHeightHalf(screenHeight / 4 * 3);
    //     quickToGetCorrectContentToClick_findNormal(info);
    // }
};


/**
 * 快速查找对应的元素信息
 */
quickToGetCorrectContentToClick = function (info) {
    logi("进入主页，滑动");
    sleep(1000);
    moveToScreenHeightHalf(screenHeight / 7 * 6);
    logd("-00")

    // find TOP
    quickToGetCorrectContentToClick_findTOP(info);

    if (!info.personal_center_topIsClicked) {
        quickToGetCorrectContentToClick_findNormal(info);
    }
};

/**
 * action :一般情况下 按下为0，弹起为1，移动为2
 *
 * @param yPostion 起始位置
 */
moveToScreenHeightHalf = function (yPostion) {
    var touch1 = [];
    var screenHeightHalf = getScreenHeight() / 2;
    // 按下的初始坐标
    touch1.push({"action": 0, "x": "320", "y": yPostion, "pointer": 1, "delay": 200});
    // 手指弹起的坐标，也就是最终的坐标
    touch1.push({"action": 1, "x": "320", "y": screenHeightHalf, "pointer": 1, "delay": 200});
    // 移动的坐标
    touch1.push({"action": 2, "x": "320", "y": screenHeightHalf, "pointer": 1, "delay": 200});
    multiTouch(touch1, null, null, 1000);
};

/**
 * 支持上下滑动
 * action :一般情况下 按下为0，弹起为1，移动为2
 *
 * @param num 次数
 * @param yPostion 起始位置
 */
moveToScreenHeightHalfByTimes = function (num, yPostion) {
    if (null != num) {
        for (var i = 0; i < num; i++) {
            sleep(moveToUpOrDownIntervalTime);
            moveToScreenHeightHalf(yPostion);
            sleep(moveToUpOrDownIntervalTime);
        }
    }
};

/**
 * 支持左右滑动
 *
 * @param screenWidthHalfRight 滑动到右边的X
 * @param screenWidthHalfLeft 滑动到左边的X
 * @param screenHeightHalfMore 在什么位置的Y进行滑动
 */
moveToLeftOrRight = function (screenWidthHalfRight, screenWidthHalfLeft, screenHeightHalfMore) {
    var touch1 = [];
    // 按下的初始坐标
    touch1.push({
        "action": 0,
        "x": screenWidthHalfRight,
        "y": screenHeightHalfMore,
        "pointer": 1,
        "delay": 200
    });
    // 手指弹起的坐标，也就是最终的坐标
    touch1.push({
        "action": 1,
        "x": screenWidthHalfLeft,
        "y": screenHeightHalfMore,
        "pointer": 1,
        "delay": 200
    });
    // 移动的坐标
    touch1.push({
        "action": 2,
        "x": screenWidthHalfLeft,
        "y": screenHeightHalfMore,
        "pointer": 1,
        "delay": 200
    });
    multiTouch(touch1, null, null, 1000);
};

moveToLeftOrRightByTimes = function (num, screenWidthHalfRight, screenWidthHalfLeft, screenHeightHalfMore) {
    if (null != num) {
        for (var i = 0; i < num; i++) {
            sleep(moveToLeftOrRightIntervalTime);
            moveToLeftOrRight(screenWidthHalfRight, screenWidthHalfLeft, screenHeightHalfMore);
            sleep(moveToLeftOrRightIntervalTime);
        }
    }
};

/**
 * 向左滑动
 *
 * @param num 滑动的次数
 */
moveToLeft = function (num) {
    var screenWidthHalfRight = screenWidth / 6 * 5;
    var screenWidthHalfLeft = screenWidth / 6;
    var screenHeightHalfMore = screenHeight / 6;
    moveToLeftOrRightByTimes(num, screenWidthHalfRight, screenWidthHalfLeft, screenHeightHalfMore);
};

/**
 * 向右滑动
 *
 * @param num 滑动的次数
 */
moveToRight = function (num) {
    var screenWidthHalfRight = screenWidth / 6;
    var screenWidthHalfLeft = screenWidth / 6 * 5;
    var screenHeightHalfMore = screenHeight / 6;
    moveToLeftOrRightByTimes(num, screenWidthHalfRight, screenWidthHalfLeft, screenHeightHalfMore);
};

/**
 * 刚出来的需要返回文章 重新选择
 */
newArticleNeedBack = function () {
    // 先看看能不能获取到时间
    var nodeInfo;
    do {
        nodeInfo = clz("android.widget.TextView").depth(13).index(0)
            .drawingOrder(1).getOneNodeInfo(2000);
        if (nodeInfo == null) {
            // 如果没有的话，就下滑一点
            moveToScreenHeightHalf(screenHeight / 6 * 5);
        }
    } while (nodeInfo == null);

    if (nodeInfo != null) {
        var time = nodeInfo.text;

    }
};


/**
 * 判断是不是视频，如果上方没有【关注】或者没有【已关注】，即可认为是视频
 * 如果是视频的话，直接等待固定的时间 {@link viewProductDetailTotalTime}
 */
isVideo = function () {
    var oneNodeInfo = clz("android.widget.TextView").depth(11)
        .index(2).drawingOrder(3).getOneNodeInfo(1000);
    if (oneNodeInfo != null) {
        if (oneNodeInfo.text === "关注" || oneNodeInfo.text === "已关注") {
            return false;
        }
    }
    return true;
};

/**
 * 点击 爱心
 */
clickLoveButton = function () {
    try {
        var node = textMatch("说点什么...");
        if (node == null) {
            var tempNode = textMatch("说点什...");
            node = tempNode == null ? textMatch("说点...") : tempNode;
        }
        if (node != null) {
            var isClick = node.getOneNodeInfo(2000).nextSiblings()[0].click();
            if (isClick) {
                logi("点赞完成！");
            } else {
                logi("点赞失败！");
            }
        } else {
            loge("点赞失败，null");

        }
    } catch (e) {
        loge("点赞失败！！{}", e);
    }
};

/**
 * 为视频详情 进行点赞
 */
clickLoveButtonForVideo = function () {
    try {
        var node = textMatch("发弹幕");
        if (node == null) {
            node = textMatch("发弹");
        }
        if (node != null) {
            var isClick = node.getOneNodeInfo(2000).nextSiblings()[0].click();
            if (isClick) {
                logi("点赞完成！");
            } else {
                logi("点赞失败！");
            }
        } else {
            loge("点赞失败，null");
        }
    } catch (e) {
        loge("点赞失败！！{}", e);
    }
};


var currentAlbumNameNode = new NodeInfo();
var temp_currentAlbumName;
getCurrentAlbumName = function () {
    currentAlbumNameNode = new NodeInfo();
    temp_currentAlbumName = "";
    if (global_albumName === "") {
        currentAlbumNameNode = clz("android.widget.RelativeLayout")
            .depth(10).getOneNodeInfo(1000);
        temp_currentAlbumName = clz("android.widget.TextView").depth(12)
            .drawingOrder(1).index(0).getOneNodeInfo(1000).text;
    } else {
        var nodeInfos = clz("android.widget.TextView").depth(12).getNodeInfo(1000);
        for (var i = 0; i < nodeInfos.length; i++) {
            var value = nodeInfos[i].text;
            if (value === global_albumName) {
                currentAlbumNameNode = nodeInfos[i];
                temp_currentAlbumName = value;
                break;
            }
        }
        if (temp_currentAlbumName === "") {
            currentAlbumNameNode = clz("android.widget.RelativeLayout")
                .depth(10).getOneNodeInfo(1000);
            temp_currentAlbumName = clz("android.widget.TextView").depth(12)
                .drawingOrder(1).index(0).getOneNodeInfo(1000).text;
        }
    }
};

clickCollectArticleButton_V2 = function () {
    try {
        var node = textMatch("说点什么...");
        if (node == null) {
            var tempNode = textMatch("说点什...");
            node = tempNode == null ? textMatch("说点...") : tempNode;
        }
        if (node != null) {
            var collection = node.getOneNodeInfo(2000).nextSiblings()[1];
            // TODO 是否收藏过
            var isClick = collection.click();
            if (isClick) {
                sleep(500);
                var categoryTemp = clz("android.widget.TextView").depth(7).index(2)
                    .drawingOrder(3).getNodeInfo(2000);
                if (categoryTemp != null) {
                    for (var i = 0; i < categoryTemp.length; i++) {
                        var value1 = categoryTemp[i];
                        if (value1.text === "加入专辑") {
                            value1.click();
                            sleep(1000 * 2);
                            getCurrentAlbumName();
                            logi("加入专辑 【" + temp_currentAlbumName + "】");
                            sleep(1000 * 2);
                            currentAlbumNameNode.click();
                            logi("收藏完成");
                            sleep(1000 * 2);
                            break;
                        }
                    }
                }
            } else {
                loge("收藏失败。。。");
            }
        } else {
            loge("收藏失败，null");

        }
    } catch (e) {
        loge("收藏失败！！{}", e);
    }
};

/**
 * 点击 收藏
 */
clickCollectArticleButton = function () {
    var nodeInfo = clz("android.widget.ImageView")
        .depth(12).index(0).drawingOrder(1).getNodeInfo(2000);
    if (nodeInfo != null) {
        for (var i = 0; i < nodeInfo.length; i++) {
            var value = nodeInfo[i];
            if (value.parent() != null
                && value.parent().clz === "android.widget.LinearLayout"
                && value.parent().index === 3
                && value.parent().drawingOrder === 4) {
                // 如果已经点击过了，就不用点了
                if (!value.selected) {
                    // 开始收藏
                    value.click();
                    sleep(500);
                    var categoryTemp = clz("android.widget.TextView").depth(7).index(2)
                        .drawingOrder(3).getNodeInfo(2000);
                    if (categoryTemp != null) {
                        for (var i = 0; i < categoryTemp.length; i++) {
                            var value1 = categoryTemp[i];
                            if (value1.text === "加入专辑") {
                                value1.click();
                                sleep(1000 * 2);
                                getCurrentAlbumName();
                                logi("加入专辑 【" + temp_currentAlbumName + "】");
                                sleep(1000 * 2);
                                currentAlbumNameNode.click();
                                logi("收藏完成");
                                sleep(1000 * 2);
                                break;
                            }
                        }
                    }
                } else {
                    logi("该文章已经收藏过啦！！");
                }
                break;
            } else {
                // TODO
            }
        }
    } else {
        // TODO
    }
};

/**
 * 点击 收藏 ，需要操作大概 6s 的时间
 */
clickCollectArticleButtonForVideo = function () {
    var nodeInfo = clz("android.widget.ImageView")
        .depth(12).index(0).drawingOrder(1).getNodeInfo(2000);
    if (nodeInfo != null) {
        for (var i = 0; i < nodeInfo.length; i++) {
            var value = nodeInfo[i];
            if (value.parent() != null
                && value.parent().clz === "android.widget.LinearLayout"
                && value.parent().index === 20
                && value.parent().drawingOrder === 25) {
                // 如果已经点击过了，就不用点了
                if (!value.selected) {
                    // 开始收藏
                    value.click();
                    sleep(500);
                    var categoryTemp = clz("android.widget.TextView").depth(7).index(2)
                        .drawingOrder(3).getNodeInfo(2000);
                    if (categoryTemp != null) {
                        for (var i = 0; i < categoryTemp.length; i++) {
                            var value1 = categoryTemp[i];
                            if (value1.text === "加入专辑") {
                                value1.click();
                                sleep(1000 * 2);
                                getCurrentAlbumName();
                                logi("加入专辑 【" + temp_currentAlbumName + "】");
                                sleep(1000 * 2);
                                currentAlbumNameNode.click();
                                logi("收藏完成");
                                sleep(1000 * 2);
                                break;
                            }
                        }
                    }
                } else {
                    logi("该文章已经收藏过啦！！");
                }
                break;
            } else {
                // TODO
            }
        }
    } else {
        // TODO
    }
};


isNeedAttention = function () {
    // 判断是否是视频还是图文详情
    if (isVideo()) {
        var videoTempNode = id("com.xingin.xhs:id/matrixFollowView").depth(11)
            .drawingOrder(22).getNodeInfo(2000);
        for (var i = 0; i < videoTempNode.length; i++) {
            var value = videoTempNode[i].text;
            if (value === "关注") {
                videoTempNode[i].click();
                logi("关注成功");
                break;
            }
            if (value === "已关注") {
                logi("已经关注过");
                break;
            }
        }
    } else {
        var oneNodeInfo = clz("android.widget.TextView").depth(11)
            .index(2).drawingOrder(3).getNodeInfo(2000);
        for (var i = 0; i < oneNodeInfo.length; i++) {
            var value = oneNodeInfo[i].text;
            if (value === "关注") {
                oneNodeInfo[i].click();
                logi("关注成功");
                break;
            }
            if (value === "已关注") {
                logi("已经关注过");
                break;
            }
        }
    }
};


//////////////// 详情页面时间 //////////////////////////
// 关注和点赞间隔时长（默认2s）
var attentionAndLikeWaitTime = 1000 * 2;

// 点赞和收藏间隔时长（默认5s）
var likeAndCollectWaitTime = 1000 * 2;
//////////////// 详情页面时间 //////////////////////////

/**
 * 视频操作
 */
viewArticle_Video = function () {
    logi("视频文章，等待 " + viewProductDetailTotalTime / 1000 + "秒，请稍候")
    sleep(viewProductDetailTotalTime - 1000 * 2);

    // 判断是否需要关注
    isNeedAttention();
    sleep(attentionAndLikeWaitTime);
    // 开始点击爱心
    clickLoveButtonForVideo();
    // 默认为false，只能点赞
    if (isLoveOrCollectArticle) {
        sleep(likeAndCollectWaitTime);
        // 花费 6s 的时间
        clickCollectArticleButtonForVideo();
        sleep(1000 * 2);
    }
};

/**
 * 固定时间：2 + 8 + 2 + 2 = 14s
 */
viewArticle_Normal_Action_Left_Right = function () {
    logi("向左滑动，稍后");
    // 先等待2秒钟
    sleep(1000 * 2);
    // 滑动图片
    moveToLeft(4);
    sleep(1000 * 2);
    logi("向右滑动，稍后");
    moveToRight(1);
};

/**
 * 固定时间 2+6+2+2+2=14s
 */
viewArticle_Normal_Action_Up_Down = function () {
    // 向下滑动
    sleep(2000);
    logi("向下滑动，稍后");
    moveToScreenHeightHalfByTimes(3, screenHeight / 4 * 3.5);
    // 向上滑动
    sleep(2000);
    logi("向上滑动，稍后");
    moveToScreenHeightHalfByTimes(1, screenHeight / 4);
    sleep(2000);
};
/**
 * 2(固定) + 14s + 14s = 30s
 */
viewArticle_Normal = function () {
    logi("操作图文详情");

    // 固定时间：14s
    // viewArticle_Normal_Action_Left_Right();

    // 固定时间：14s
    // viewArticle_Normal_Action_Up_Down();

    if (viewProductDetailTotalTime > 40 * 1000) {
        sleep(viewProductDetailTotalTime - 40 * 1000);
    }
    // 判断是否需要关注
    isNeedAttention();
    sleep(attentionAndLikeWaitTime);
    // 点击爱心
    clickLoveButton();
    // 默认为false，只能点赞
    if (isLoveOrCollectArticle) {
        sleep(likeAndCollectWaitTime);
        clickCollectArticleButton_V2();
        // clickCollectArticleButton();
        sleep(1000 * 2);
    }
};

/**
 * 图文详情的浏览
 *  2 +
 */
viewArticle = function () {
    // 进来之后，进行等待2秒
    sleep(1000 * 2);

    // 判断是不是视频
    if (isVideo()) {
        viewArticle_Video();
    } else {
        viewArticle_Normal();
    }
    sleep(1500);
    logi("66565222")
    back();
};

/**
 * 如果是自己的话 就直接跳过
 *
 * @returns {boolean}
 * @param info
 */
isMyselfToSkip = function (info) {
    currentChildByFNode = null;
    setChildNodeByClz(info.attention_list_currentNodeInfo,
        "android.widget.TextView", 16, 4, 3);
    if (currentChildByFNode == null) {
        info.attention_list_currentNodeInfo = getAttentionNodeByNextSiblings(info.attention_list_currentNodeInfo);
        info.attention_list_isMyself = true;
        return true;
    }
    return false;
};


//////////////////////////// 主函数 START ///////////////////////////////
var local = new ThreadLocal();
local.attention_list_currentNodeInfo = getAttentionListByDrawingOrder();
var tempNum = 0;
while (tempNum < 1000) {
    tempNum++;
    if (null != local.attention_list_currentNodeInfo) {
        local.clear();

        try {
            // 先进行设置
            setCurrentCustomer(local);
            logi("即将为第 " + local.currentAccountNumber + " 位点击：" + local.currentAccountName);
        } catch (e) {
            loge("setCurrentCustomer error {}", e);
        }

        try {
            // 如果是自己 就不进行点击
            if (isMyselfToSkip(local)) {
                logd("this is myself ,need not to click..");
                continue;
            }
        } catch (e) {
            loge("isMyselfToSkip error {}", e);
        }

        local.attention_list_currentNodeInfo.click();
        clickedTime++;
        sleep(1000);

        // =========== 找到正确的图文点击进去 START ===========
        logi("查找合适的文章");
        // quickToGetCorrectContentToClick_times = 0;
        // quickToGetCorrectContentToClick_boolean = false;
        quickToGetCorrectContentToClick(local);
        // =========== 找到正确的图文点击进去 END =============
        if (local.personal_center_hasFindRightProduct) {
            // =========== 进行浏览图文信息，需要返回 START =========
            logi("进入详情页");
            viewArticle();
            // =========== 进行浏览图文信息，需要返回 END ===========
        }

        ///////////////////////////
        sleep(1500);
        back();
        sleep(1500);

        // 移动关注列表中的元素
        moveToScreenHeightHalf(local.attention_list_currentNodeInfo.bounds.bottom - 60);

        sleep(1500);
        local.attention_list_currentNodeInfo = getAttentionNodeByNextSiblings(local.attention_list_currentNodeInfo);

        logi("第 " + local.currentAccountNumber + " 位点击完成：" + local.currentAccountName)

    } else {
        logi("关注列表没有人了。。")
        logi("结束，共计 [" + clickedCustomer.length - 1 + "] 个人");
        logd("list have invoked are " + JSON.stringify(clickedCustomer));
        break;
    }
}

exit();

